import subprocess 

# ffmpeg -allowed_extensions ALL -protocol_whitelist "file,http,crypto,tcp" -i 5a8182c402efdef121f7882722b96be0.m3u8 -c copy output.mp4
  
# 设置FFmpeg命令  

def m3u8ToMp4(m3u8Path, mp4Path):

	ffmpeg_command = f"ffmpeg -allowed_extensions ALL -protocol_whitelist \"file,http,crypto,tcp\" -i {m3u8Path} -c copy {mp4Path}"  
	
	print('=== ffmpeg开始转码 ===')
	print('m3u8路径： '+m3u8Path)
	print('输出mp4路径： '+mp4Path)
	# 使用subprocess调用FFmpeg并捕获输出  
	process = subprocess.Popen(ffmpeg_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)  
	stdout, stderr = process.communicate()  
	
	# 检查命令是转码行成功  
	if process.returncode == 0:  
	    print("=== ffmpeg转码完成 ====")    
	    print(stdout.decode())  
	    return True
	else:  
	    print("=== 转换失败！=== ")  
	    print(stderr.decode())
	    return False

# 示例用法
# m3 = '/Users/cgw/Desktop/1113/py/downTemp/真的来了/index.m3u8'
# m4 = '/Users/cgw/Desktop/1113/py/downTemp/真的来了/index.mp4'
# m3u8ToMp4(m3, m4)